<?php

namespace Form\Settings\Task;


use App\Controller\Admin\Settings\Task\RefreshController;
use App\Dao\Settings\SettingsTaskRefreshDao;
use App\Dao\Task\TaskHelpDao;
use Form\BaseForm;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\Elm;


/**
 * @ApiController(prefix="form/settings/task/refresh")
 */
class Refresh extends BaseForm
{
    protected $yjCode = 'help';


    public function __construct()
    {
        App(RefreshController::class);
    }

    protected $routes = [
        'lists' => 'admin/settings/task/refresh/lists',
        'add' => 'admin/settings/task/refresh/add',
        'edit' => 'admin/settings/task/refresh/edit',
        'delete' => 'admin/settings/task/refresh/del',
        'form' => 'form/settings/task/refresh/update',
    ];


    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('价钱')->setProp('price'),
            Column::txt()->setLabel('刷新次数')->setProp('num'),
        ]);
        return $colum->toArray();
    }

    protected function getRoute($types = null)
    {
        if (is_null($types)) {
            return $this->routes;
        }

        return $this->routes[$types];
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $routes = $this->getRoute();
        $column = $this->getColumn();
        _SUCCESS(compact('routes', 'column'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getValidatorData('id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));

        $form->setRule([
            Elm::number('price', '价钱')->required(),
            Elm::number('num', '数量')->required(),
        ]);

        $id && $form->append(
            Elm::hidden('id', $id)
        );

        $lists = $form->setTitle($id ? '编辑' : '添加' . '刷新道具')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(SettingsTaskRefreshDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }


}